package com.unience.ec2.eclipse.dialog;
import com.cloudgarden.resource.SWTResourceManager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.CharBuffer;
import java.util.ArrayList;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;	
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

import com.unience.ec2.eclipse.config.Config;
import com.unience.ec2.eclipse.config.UtilSWT;
import com.xerox.amazonws.ec2.GroupDescription;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class NewEc2InstanceDialog extends org.eclipse.swt.widgets.Dialog {

	private Shell dialogShell;
	private Label lb_amiName;
	private Label label_Group;
	private Label label_user_data;
	private Combo combo_keyName;
	private Label label_keyName;
	private Button buttonCancel;
	private Button buttonAccept;
	private Label label_userData;
	private List list_SecurityGroup;
	private Text text_ami;
	
	//Variables de datos..
	
	private String keyName;
	private String ami;
	private String userData;
	private java.util.List<String> secGroupList;
	private java.util.List<String> keysList;
	private Text textUserData;

	private Button buttonFileButton;

	private boolean accept = false;
	
	static String filterPath = "C:/";

	/**
	* Auto-generated main method to display this 
	* org.eclipse.swt.widgets.Dialog inside a new Shell.
	*/
	public static void main(String[] args) {
		try {
			Display display = Display.getDefault();
			Shell shell = new Shell(display);
			NewEc2InstanceDialog inst = new NewEc2InstanceDialog(shell, SWT.NULL);
			inst.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public NewEc2InstanceDialog(Shell parent, int style) {
		
		super(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		keyName = "";
		ami = "";
		userData = "";
		secGroupList = new ArrayList<String>();
		keysList =  new ArrayList<String>();
		
		
	}

	public void open() {
		try {
			Shell parent = getParent();
			dialogShell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);

			{
				//Register as a resource user - SWTResourceManager will
				//handle the obtaining and disposing of resources
				SWTResourceManager.registerResourceUser(dialogShell);
			}
			
			

			FormLayout dialogShellLayout = new FormLayout();
			dialogShell.setLayout(null);
			dialogShell.layout();
			dialogShell.pack();			
			dialogShell.setSize(558, 340);
			dialogShell.setText("Starting AMI : " + ami);

			{
				combo_keyName = new Combo(dialogShell, SWT.BORDER);
				FormData combo_keyNameLData = new FormData();
				combo_keyNameLData.width = 155;
				combo_keyNameLData.height = 25;
				combo_keyNameLData.left =  new FormAttachment(0, 1000, 85);
				combo_keyNameLData.top =  new FormAttachment(0, 1000, 151);
				combo_keyName.setLayoutData(keysList);
				
				String [] items = new String[keysList.size()];
				int i = 0;
				for (String s:keysList){					
					items[i]= s;
					i++;
				}
				
				combo_keyName.setItems(items);
				combo_keyName.setBounds(362, 190, 162, 25);

			}
			{
				label_keyName = new Label(dialogShell, SWT.NONE);
				label_keyName.setText("keyName");
				label_keyName.setBounds(286, 198, 64, 17);
			}
			{
				buttonCancel = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
				buttonCancel.setText("Cancel");
				buttonCancel.setBounds(457, 261, 77, 27);
				buttonCancel.addSelectionListener(new SelectionAdapter() {
				      public void widgetSelected(SelectionEvent event) {
				        //input = null;
				        dialogShell.close();
				      }
				    });
			}
			{
				buttonAccept = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
				buttonAccept.setText("Accept");
				buttonAccept.setBounds(386, 261, 65, 27);
				buttonAccept.addSelectionListener(new SelectionAdapter() {
				      public void widgetSelected(SelectionEvent event) {
				    	accept = true;
				        userData = textUserData.getText();
				        keyName = combo_keyName.getText();
				        String[] secSel = list_SecurityGroup.getSelection();
				        secGroupList = new ArrayList<String>();
				        for (int i = 0; i< secSel.length;i++){
				        	secGroupList.add(secSel[i]);
				        }
				        dialogShell.close();
				      }
				    });
			}
			{
				label_user_data = new Label(dialogShell, SWT.NONE);
				label_user_data.setText("User-Data");
				label_user_data.setBounds(0, 39, 79, 17);
			}
			{
				FormData list_SecurityGroupLData = new FormData();
				list_SecurityGroupLData.width = 187;
				list_SecurityGroupLData.height = 81;
				list_SecurityGroupLData.left =  new FormAttachment(0, 1000, 85);
				list_SecurityGroupLData.top =  new FormAttachment(0, 1000, 58);
				list_SecurityGroup = new List(dialogShell, SWT.BORDER);
				list_SecurityGroup.setBounds(74, 171, 193, 90);
				//list_SecurityGroup.setLayoutData(secGroupList);				
				String [] items = new String[secGroupList.size()];
				int i = 0;
				for (String s:secGroupList){					
					items[i]= s;
					i++;
				}
				list_SecurityGroup.setItems(items);
			}
			{
				label_Group = new Label(dialogShell, SWT.NONE);
				label_Group.setText("Sec. Group");
				label_Group.setBounds(0, 171, 68, 17);
			}
			{
				text_ami = new Text(dialogShell, SWT.NONE);
				text_ami.setText(ami);
				text_ami.setEditable(false);
				text_ami.setBounds(84, 6, 185, 17);
			}
			{
				lb_amiName = new Label(dialogShell, SWT.NONE);
				lb_amiName.setText("Ami");
				lb_amiName.setBounds(0, 6, 72, 17);
			}
			{
				buttonFileButton = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
				buttonFileButton.setText("...");
				buttonFileButton.setBounds(499, 133, 39, 26);
				buttonFileButton.addMouseListener(new MouseAdapter() {
					public void mouseDown(MouseEvent evt) {
						buttonFileButtonMouseDown(evt);
					}
				});
			}
			{
				textUserData = new Text(dialogShell, SWT.MULTI | SWT.WRAP |SWT.BORDER);
				textUserData.setBounds(79, 39, 408, 122);
			}
			dialogShell.setLocation(getParent().toDisplay(100, 100));
			dialogShell.pack();		   
			dialogShell.open();
			Display display = dialogShell.getDisplay();
			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private Label getLabel_userData(Composite parent) {
		if(label_userData == null) {
			label_userData = new Label(parent, SWT.NONE);
			label_userData.setText("User-Data");
		}
		return label_userData;
	}

	public String getKeyName() {
		return keyName;
	}

	public void setKeyName(String keyName) {
		this.keyName = keyName;
	}

	public String getAmi() {
		return ami;
	}

	public void setAmi(String ami) {
		this.ami = ami;
	}

	public String getUserData() {
		return userData;
	}

	public void setUserData(String userData) {
		this.userData = userData;
	}

	public java.util.List<String> getSecGroupList() {
		return secGroupList;
	}

	public void setSecGroupList(java.util.List<String> secGroupList) {
		this.secGroupList = secGroupList;
	}

	public java.util.List<String> getKeysList() {
		return keysList;
	}

	public void setKeysList(java.util.List<String> keysList) {
		this.keysList = keysList;
	}

	public boolean isAccept() {
		return accept;
	}
	
	private void buttonFileButtonMouseDown(MouseEvent evt) {
		System.out.println("buttonFileButton.mouseDown, event="+evt);
		
		FileDialog fd = new FileDialog(this.getParent(), SWT.OPEN);
        fd.setText("Open");
        fd.setFilterPath(filterPath);
        String[] filterExt = {"*.properties", "*.sh" };
        fd.setFilterExtensions(filterExt);
        
        
        String selected = fd.open();
        
        File f = new File(selected);
        
        filterPath = f.getAbsolutePath();
        
        System.out.println(selected);
        try {
			FileReader inputFileReader = new FileReader(selected);
			
			 BufferedReader inputStream   = new BufferedReader(inputFileReader);
			 
			 String inLine = null;
			 
			 StringBuffer buff = new StringBuffer();

	            while ((inLine = inputStream.readLine()) != null) {
	                buff.append(inLine).append("\n");
	            }

			
			textUserData.setText(buff.toString());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
	}

}
